const router = require('koa-router')();
const requestReport = require('../../app/mws/reports/RequestReport');
const getReportRequestList = require('../../app/mws/reports/GetReportRequestList');

router.prefix('/api/v1/reports');
router.get('/', function (ctx, next) {
  ctx.body = 'this is a requestReport response!'
});

router.post('/request/:reportType', async (ctx, next) => {
  const sellerId = ctx.headers.sellerid;
  const mwsAuthToken = ctx.headers.mwsauthtoken;
  const reportType = ctx.params.reportType;

  try {
    let json = await requestReport.requestReport(sellerId, mwsAuthToken, reportType);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

router.get('/request', async (ctx, next) => {
  const sellerId = ctx.headers.sellerid;
  const mwsAuthToken = ctx.headers.mwsauthtoken;
  const reportType = ctx.query.reportType;

  try {
    let json = await getReportRequestList.getReportRequestListByReportType(sellerId, mwsAuthToken, [reportType]);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

module.exports = router;